package com.example.my0609projects;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.my0609projects.bean.Mystl;

import org.w3c.dom.Text;

import java.util.List;

public class MyAdpater extends BaseAdapter {
    List<Mystl> list;
    Context context;

    public MyAdpater(List<Mystl> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list!=null?list.size():0;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return list.get(position).getType();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }
    static class ViewHedle1{
    ImageView imageView;
    TextView tv1;
    TextView tv2;
    TextView tv3;
    }
    static  class  ViewHedle2{
    ImageView img1,img2,img3;
    TextView tv_title,tv_author,tv_time;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHedle1 vh1;
        ViewHedle2 vh2;
        int type=getItemViewType(position);
        Mystl mystl=list.get(position);
        if (convertView==null){
            switch (type){
                case 0:
                    vh1=new ViewHedle1();
                    convertView= LayoutInflater.from(context).inflate(R.layout.list_item1,null);
                    vh1.tv1=convertView.findViewById(R.id.tv1);
                    vh1.tv2=convertView.findViewById(R.id.tv2);
                    vh1.tv3=convertView.findViewById(R.id.tv3);
                    vh1.tv1.setText(mystl.getTitle());
                    vh1.tv2.setText(mystl.getAuthor());
                    vh1.tv3.setText(mystl.getDate());
                    convertView.setTag(vh1);
                    break;
                case 1:
                    vh2=new ViewHedle2();
                    convertView= LayoutInflater.from(context).inflate(R.layout.list_item2,null);
                    vh2.tv_title=convertView.findViewById(R.id.tv_title);
                    vh2.tv_author=convertView.findViewById(R.id.tv_author);
                    vh2.tv_time=convertView.findViewById(R.id.tv_time);
                    vh2.tv_title.setText(mystl.getTitle());
                    vh2.tv_author.setText(mystl.getAuthor());
                    vh2.tv_time.setText(mystl.getDate());
                    convertView.setTag(vh2);
                    break;
            }

        }else{
            switch (type){
                case 0:
                    vh1= (ViewHedle1) convertView.getTag();
                    break;
                case 1:
                    vh2= (ViewHedle2) convertView.getTag();
            }
        }
        return convertView;
    }

}
